﻿using System;
using System.ComponentModel;
using BlueDot.Data.Core;
using BlueDot.Mobile.Core;
using CancelWorkFlowStep.Controllers;
using CancelWorkFlowStep.Model;

namespace CancelWorkFlowStep.Views
{
    public partial class MainView : ViewBase
    {
        #region Fields

        private const string MainViewShown = WellKnownActions.ViewShown + "/MainView";
        private const string MainViewShowning = WellKnownActions.ViewShowing + "/MainView";

        #endregion

        #region Constructors

        public MainView()
        {
            InitializeComponent();
        }

        [DependencyConstructor]
        public MainView(IMobileApplication application)
            : base(application)
        {
            InitializeComponent();
        }

        #endregion

        #region Properties

        [DependencyProperty(CreateNew = true)]
        public MainController MainController { get; set; }

        #endregion

        #region public

        /// <summary>
        /// This action receiver is called when MainView is shown.
        /// </summary>
        [Receiver(MainViewShown)]
        public void OnMainViewShown(object sender, EventArgs e)
        {
        }

        /// <summary>
        /// This action receiver is called when MainView is showing.
        /// </summary>
        [Receiver(MainViewShowning)]
        public void OnMainViewShowning(object sender, EventArgs e)
        {
            ((Shell) Application.Shell).Menu = mainMenu;
            BindUsers();
        }

        #endregion

        #region private

        private void BindUsers()
        {
            EntityDataView<User> users = MainController.GetUsers();

            var userList = (BindingList<User>) users.EntityList;

            userBindingSource.DataSource = userList;
        }

        #endregion
    }
}